/** @file 
 *	@class Input
 *  @brief A controller that handles input from the player's mouse and keyboard
 *
 *  The StateManager inherits from the Ogre Framelistener, and uses the
 *	frameRenderingQueued method to run through a list of controllers
 *	to make sure they are updated
 */

#include "stdafx.h"
#include "AvatarController.h"
#include "MoveCommand.h"
#include "ViewCommand.h"
#include "SpawnCommand.h"
#include "InputManager.h"
#include "CommandList.h"
#include "Vector3.h"
#include "MovePlayerCommand.h"
#include "ShootCommand.h"

AvatarController::AvatarController( NetworkController* nc) {
	InputManager::getInstance()->registerObserver(this);
	m_playerAvatar = NULL;
	m_lastDeltaAngle = 0;

	m_lastMouseX = -1;
	m_lastMouseY = -1;
}

AvatarController::~AvatarController() {}

void AvatarController::setPlayerAvatar(PlayerAvatar* avatar) {
	m_playerAvatar = avatar;
	player->getMusicController()->setMainAvatar(avatar);
	player->playerSpawn();
	
}


bool AvatarController::update(float deltaTime) {
	OIS::Keyboard* keyboard = InputManager::getInstance()->getKeyboard();
   
  if(keyboard->isKeyDown(OIS::KC_T)) {
		CommandList::getInstance()->getNetworkController()->c->disconnect();
		return false;
	}

	if(m_playerAvatar) {
		float dX = 0;
		float dY = 0;
		bool wantJump = false;

		if(keyboard->isKeyDown(OIS::KC_W)) {
			dX = 1;
			
		}

		if(keyboard->isKeyDown(OIS::KC_A)) {
			dY = 1;
		}

		if(keyboard->isKeyDown(OIS::KC_S)) {
			dX = -1;
		}

		if(keyboard->isKeyDown(OIS::KC_D)) {
			dY = -1;
		}

		if(keyboard->isKeyDown(OIS::KC_SPACE)) {
			wantJump = true;
		} 

		OIS::Mouse* mouse = InputManager::getInstance()->getMouse();
		
		m_commands.push_back(new MovePlayerCommand(dX, dY, deltaTime, wantJump, m_playerAvatar->getID()));

	}
	
	CommandList::getInstance()->addList(m_commands);
	
	m_commands.clear();

	return true;
}

bool AvatarController::keyPressed(const OIS::KeyEvent &arg) {
	if(m_playerAvatar) {
		if(arg.key == OIS::KC_Q) {
			m_playerAvatar->damage(10);
		}
		
		if (arg.key == OIS::KC_K) {
			m_playerAvatar->setHealth(0);
			player->playerDeath();
		}
		
		if (arg.key == OIS::KC_L) {
			m_commands.clear();
			m_playerAvatar->setHealth(100);
		}

		if (arg.key == OIS::KC_H) {
			//m_commands.push_back(new SpawnCommand(5,5,5,10,false));
		}
	}
	
	return true;
}

bool AvatarController::keyReleased(const OIS::KeyEvent &arg) {
	return true;
}
	
bool AvatarController::mouseMoved( const OIS::MouseEvent &arg ) {
	if(m_playerAvatar) {
		m_commands.push_back(new ViewCommand(-arg.state.X.rel, arg.state.Y.rel,m_playerAvatar->getID()));
	}

	return true;
}

bool AvatarController::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {
	if(id == OIS::MB_Left && m_playerAvatar) {
		m_commands.push_back(new ShootCommand(m_playerAvatar->getID()));
	}

	return true;
}

bool AvatarController::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {
	return true;
}

PlayerAvatar* AvatarController::getPlayerAvatar() {
	return m_playerAvatar;
}

void AvatarController::setActive(bool in) {
	m_active = in;
	inputActive = in;
}

void AvatarController::setPlayer(Player* p) {
	player = p;
}